package cn.tedu.antconlonysever.contract.service.impl;

import cn.tedu.antcolonycommons.ex.ServiceException;
import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.ServiceCode;
import cn.tedu.antconlonysever.contract.dao.persist.repository.IContactRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.Contact;
import cn.tedu.antconlonysever.contract.pojo.param.ContactAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.ContactUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.vo.ContactListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.ContactStandardVO;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractListVo;
import cn.tedu.antconlonysever.contract.service.IContactService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class ContactServiceImpl implements IContactService {
    @Value("${antcolony.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IContactRepository contactRepository;
    @Override
    public void addNew(ContactAddNewParam contactAddNewParam) {
        Long cusId = contactAddNewParam.getCusId();
        int count = contactRepository.countCusId(cusId);
        if(count>0){
            String message = "添加客户会面失败，客户会面id已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Contact contact=new Contact();
        BeanUtils.copyProperties(contactAddNewParam,contact);
        contact.setGmtCreate(LocalDateTime.now());
        contact.setGmtModified(LocalDateTime.now());
        int rows = contactRepository.insert(contact);
        if(rows!=1){
            String message = "添加客户会面失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("将新的数据插入到数据库，完成！");
    }

    @Override
    public void delete(Long id) {
        ContactStandardVO standardById = contactRepository.getStandardById(id);
        if(standardById==null){
            String message = "删除客户会面失败，客户会面ID不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = contactRepository.deleteById(id);
        if(rows!=1){
            String message = "删除客户会面失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }


    }

    @Override
    public void updateInfoById(Long id, ContactUpdateParam contactUpdateParam) {
        ContactStandardVO standardById = contactRepository.getStandardById(id);
        if(standardById==null){
            String message = "修改客户会面失败，客户会面ID不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int count = contactRepository.contactUsIdNotId(id, contactUpdateParam.getCusId());
        if(count>0){
            String message = "修改客户会面失败，尝试修改会面ID已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Contact contact=new Contact();
        BeanUtils.copyProperties(contactUpdateParam,contact);
        contact.setId(id);
        int rows = contactRepository.update(contact);
        if(rows!=1){
            String message = "修改客户会面失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }


    }

    @Override
    public ContactStandardVO getStandardById(Long id) {
        ContactStandardVO queryResult = contactRepository.getStandardById(id);
        if(queryResult==null){
            String message = "根据ID查询会面详情失败，会面详情数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<ContactListVO> list(Integer pageNum) {
        PageData<ContactListVO> pageData = contactRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<ContactListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询合同列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<ContactListVO> pageData =  contactRepository.list(pageNum, pageSize);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }
}
